package 剑指offer.重建二叉树_4;

public class Search {
	public static void main(String[] args){
		int[] a = new int[]{4,2};
		System.out.println(binarySearch0(a,0,2,2));
	}
	
    private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) {
    	int low = fromIndex;
    	int high = toIndex - 1;

    	while (low <= high) {
    		int mid = (low + high) >>> 1;
    		int midVal = a[mid];

    		if (midVal < key)
    			low = mid + 1;
    		else if (midVal > key)
    			high = mid - 1;
    		else
    			return mid; // key found
    	}
    	return -(low + 1);  // key not found.
    }
}
